package AI;

import Bots.Buffer;
import cz.cuni.amis.utils.exception.PogamutException;

import java.io.*;

public class AC {


    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //构造
    static int capacity = 9999;
    static int WriteInTarget = 50;
    static int K = 16;

    public int numState;
    public int numAction;

    public FALCON agent;
    public NN nnet;

    public AC(int agent_num, int numState, int numAction, boolean rw) {

        this.numState = numState;
        this.numAction = numAction;
        nnet = new NN(numState, numAction);
        agent = new FALCON(agent_num, numState, numAction, rw, "AC");
    }


    public int Policy(double[] stat) {
        double[] pol = new double[numAction];
        pol = nnet.policy(stat);

        //随机选一个动作
        double rand = Math.random();
        int j = 0;
        for (int i = 0; i < numAction; i++) {
            rand -= pol[i];
            if (rand > 0) {
                j++;
            } else {
                break;
            }
        }
        return j;

    }


    public double VAlpha = (double) 0.5;

    public double getVValue(double[] next_stat, double[] pol) {
        double VValue = 0.0;
        for (int i = 0; i < numAction; i++) {//Q-学习
            int action = i;
            double[] act = new double[numAction];
            agent.actiontoact(action, act);
            VValue += agent.Q_Function(next_stat, act) * pol[i];
        }
        return VValue;
    }

    public void Q_Function_update(Buffer buff, double[] p) {
        agent.Q_Function_update(buff, p);
        double y = 0;
        if (buff.end == true) {
            y = buff.r;
        } else {
            y = buff.r + getVValue(buff.next_stat, nnet.policy(buff.next_stat));
        }
        double VValue = getVValue(buff.stat, nnet.policy(buff.stat));
        double AValue = y - VValue;
        nnet.learn(buff.stat, buff.action, AValue);

    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    public void readfile(int agent_num) throws IOException {
        String path = "e:/store-" + agent_num + ".txt";
        FileInputStream in = new FileInputStream(path);
        DataInputStream din = new DataInputStream(in);


        din.close();
        in.close();

    }

    public void writefile(int agent_num) throws PogamutException, Exception {
        String path = "e:/store-" + agent_num + ".txt";
        FileOutputStream out = new FileOutputStream(path);
        DataOutputStream dout = new DataOutputStream(out);

        dout.close();
        out.close();
    }


}